//
//  ProductDetailViewController.m
//  TryShopping
//
//  Created by LiYinDi on 16/6/30.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "ProductDetailViewController.h"
#import "ShareView.h"
#import "NewMediaPromotionViewController.h"
#import "ExhibitionObject.h"
@interface ProductDetailViewController ()<ShareViewDelegate,UIGestureRecognizerDelegate>

@end

@implementation ProductDetailViewController

{
    ShareView*SV;
    UIView*shareblackview;
}
-(void)viewWillAppear:(BOOL)animated
{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    //  [[self rdv_tabBarController] setBlurViewHidden:YES];
    self.navigationController.navigationBarHidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setAutoLayout];
    
    [_urlWebView setBackgroundColor:[UIColor clearColor]];
    NSURL *url1 = [[NSURL alloc]initWithString:_url];
    //    NSLog(@"_url=====%@",_url);
    [_urlWebView loadRequest:[NSURLRequest requestWithURL:url1]];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   
    //    [self performSelector:@selector(getAchieveWebViewHeight) withObject:nil afterDelay:0.3];
    JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    //js调用iOS
    //第一种情况
    //其中test1就是js的方法名称，赋给是一个block 里面是iOS代码
    
    ExhibitionObject *exhiObject=[ExhibitionObject  new];
//    exhiObject.delegate=self;
    context[@"appJs"]=exhiObject;
    
    //
    //    NSString *jsStr2=@"appJs.gotoItemDetail('参数1')";
    //    [context evaluateScript:jsStr2];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)closeKeyBoad
{
    [self.window makeKeyWindow];
    self.window.hidden=YES;

    
}
-(void)cancel:(NSString *)cancel{


    [self closeKeyBoad];
}
- (IBAction)shareBtnClick:(id)sender {
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSString* companyId = [user objectForKey:@"companyId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"product" forKey:@"contentType"];
    [param setValue:self.itemID forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [param setValue:companyId forKey:@"companyId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
//  NSLog(@"====%@",param);
    [HttpRequest requestData:@"Company/getShareUrl" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
//           NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
                [self.window makeKeyAndVisible];
                
                shareblackview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
                shareblackview.backgroundColor=[UIColor blackColor];
                shareblackview.alpha=0.65;
                [self.window addSubview:shareblackview];
                
                SV = [[ShareView alloc]initWithFrame:CGRectMake(0, ScreenHeight-245, ScreenWidth,245)];
                SV.dataDict=severDataDict.dataDict;
                SV.mDelegate=self;
                [self.window addSubview:SV];
                
                UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
                singleFingerOne.numberOfTouchesRequired = 1; //手指数
                singleFingerOne.numberOfTapsRequired = 1; //tap次数
                singleFingerOne.delegate = self;
                [shareblackview addGestureRecognizer:singleFingerOne];
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
               
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    
}
-(void)successfullyReturns:(NSString *)successfullyReturns{//分享成功后回调登记接口
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.shareType forKey:@"contentType"];
    [param setValue:self.itemID forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Company/ShareRecords/Add" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                [self closeKeyBoad];
//                NewMediaPromotionViewController *loginVC=[[NewMediaPromotionViewController alloc] init];
//                [self.navigationController pushViewController:loginVC animated:YES];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}

-(void)setAutoLayout
{
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        make.height.equalTo(@64);
    }];
    
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_topView.mas_left);
        make.height.equalTo(@40);
        make.width.equalTo(@60);
        make.bottom.equalTo(_topView.mas_bottom);
    }];
    [_shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_topView.mas_right);
        make.height.equalTo(@40);
        make.width.equalTo(@50);
        make.bottom.equalTo(_topView.mas_bottom);
    }];
    
    [_urlWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).with.offset(-20);
        make.bottom.equalTo(self.view.mas_bottom);
    }];

}
@end
